package com.zz.webapp.MapperBuilder;

import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhouzeng on 2016/9/29.
 */
@Slf4j
public class MapperBuilder {
    public static void main(String[] args) {
        String field = "user_id,name,gender,address,hobby,industry,create_time,update_time";
        String tableName = "user_profile";
        String[] fieldArr = field.split(",");
        total(fieldArr,tableName);
    }
    public static void total(String[] fieldArr,String tableName){
        String className = tableNameTOClassName(tableName);
        System.out.println("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
        System.out.println("<!DOCTYPE mapper PUBLIC \"-//mybatis.org//DTD Mapper 3.0//EN\" \"http://mybatis.org/dtd/mybatis-3-mapper.dtd\" >");
        System.out.println("<mapper namespace=\""+className+"\">");
        resultMap(fieldArr,className);
        System.out.println();
        tb(tableName);
        System.out.println();
        colsAll();
        System.out.println();
        colsExcludeId(fieldArr);
        System.out.println();
        vals(fieldArr);
        System.out.println();
        updateSql(fieldArr);
        System.out.println();
        criteria(fieldArr);
        System.out.println();
        create(className);
        System.out.println();
        creates(fieldArr);;
        System.out.println();
        delete();
        System.out.println();
        load(className);
        System.out.println();
        list(className);
        System.out.println();
        update(className);
        System.out.println();
        paging(className);
        System.out.println();
        count();
        System.out.println();
        System.out.println("</mapper>");
    }
    public static String tableNameTOClassName(String tableName){
        String[] tbs = tableName.split("_");
        String className="";
        for(int i=0;i<tbs.length;i++){
            className+=(tbs[i].charAt(0)+"").toUpperCase()+tbs[i].substring(1,tbs[i].length());
        }
        //System.out.println(className);
        return className;
    }
    public static List<String> fieldArrToPropertyName(String[] fieldArr){
        List<String> propertyName = new ArrayList<String>();
        for (int i = 0; i < fieldArr.length; i++) {
            String[] property = fieldArr[i].split("_");
            String s = "";
            if (property.length > 1) {
                for (int j = 0; j < property.length; j++) {
                    if (j == 0) {
                        s += property[0];
                    } else {
                        s += (property[j].charAt(0) + "").toUpperCase() + property[j].substring(1, property[j].length());
                    }
                }
                propertyName.add(s);
            } else {
                propertyName.add(fieldArr[i]);
            }
        }
        return propertyName;
    }
    public static void resultMap(String[] fieldArr,String className){
        List<String> propertyName = fieldArrToPropertyName(fieldArr);
        System.out.println("<resultMap id=\""+className+"Map\" type=\""+className+"\">");
        System.out.println("<id column=\"id\" property=\"id\"/>");
        for(int i=0;i<fieldArr.length;i++){
            System.out.println("<result column=\""+fieldArr[i]+"\" property=\""+propertyName.get(i)+"\"/>");
        }
        System.out.println("</resultMap>");
    }
    public static void tb(String tableName){
        System.out.println("<sql id=\"tb\">");
        System.out.println(tableName);
        System.out.println("</sql>");
    }
    public static void colsAll(){
        System.out.println("<sql id=\"cols_all\">");
        System.out.println("id,<include refid=\"cols_exclude_id\"/>");
        System.out.println("</sql>");
    }
    public static void colsExcludeId(String[] fieldArr){
        System.out.println("<sql id=\"cols_exclude_id\">");
        for(int i=0;i<fieldArr.length;i++){
            if(i==fieldArr.length-1){
                System.out.println(fieldArr[i]);
            }else{
                System.out.print(fieldArr[i]+",");
            }
        }
        System.out.println("</sql>");
    }
    public static void vals(String[] fieldArr){
        List<String> propertyName = fieldArrToPropertyName(fieldArr);
        System.out.println("<sql id=\"vals\">");
        for(int i=0;i<propertyName.size();i++){
            if(i==propertyName.size()-1){
                System.out.println("now()");
            }else if(i==propertyName.size()-2){
                System.out.print("now(),");
            }else{
                System.out.print("#{"+propertyName.get(i)+"},");
            }
        }
        System.out.println("</sql>");
    }
    public static void updateSql(String[] fieldArr){
        List<String> propertyName = fieldArrToPropertyName(fieldArr);
        System.out.println("<!--更新的内容-->");
        System.out.println("<sql id=\"update_sql\">");
        System.out.println("<set>");
        for(int i=0;i<fieldArr.length-2;i++){
            System.out.println("<if test=\"" + propertyName.get(i) + " !=null\">" + fieldArr[i] + " =#{" + propertyName.get(i) + "},</if>");
        }
        System.out.println("update_at = now()");
        System.out.println("</set>");
        System.out.println("</sql>");
    }
    public static  void criteria(String[] fieldArr){
        List<String> propertyName = fieldArrToPropertyName(fieldArr);
        System.out.println("<!--分页的条件-->");
        System.out.println("<sql id=\"criteria\">");
        for(int i=0;i<fieldArr.length-2;i++){
            if(i==0){
                System.out.println("<if test=\""+propertyName.get(i)+" !=null\">"+fieldArr[i]+" =#{"+propertyName.get(i)+"}</if>");
            }else {
                System.out.println("<if test=\""+propertyName.get(i)+" !=null\">AND "+fieldArr[i]+" =#{"+propertyName.get(i)+"}</if>");
            }
        }
        System.out.println("</sql>");
    }
    public static void create(String className){
        System.out.println("<insert id=\"create\" parameterType=\""+className+"\" keyProperty=\"id\" useGeneratedKeys=\"true\">");
        System.out.println("INSERT INTO");
        System.out.println(" <include refid=\"tb\"/>");
        System.out.println("(<include refid=\"cols_exclude_id\"/>)");
        System.out.println("VALUES");
        System.out.println("(<include refid=\"vals\"/>)");
        System.out.println("</insert>");
    }
    public static void creates(String[] fieldArr){
        List<String> propertyName = fieldArrToPropertyName(fieldArr);
        System.out.println("<insert id=\"creates\" parameterType=\"list\">");
        System.out.println("INSERT INTO <include refid=\"tb\"/>");
        System.out.println("(<include refid=\"cols_exclude_id\"/>)");
        System.out.println("VALUES");
        System.out.println("<foreach collection=\"list\" item=\"i\" index=\"index\" separator=\",\">");
        for(int i=0;i<propertyName.size();i++){
            if(i==0){
                System.out.print("( #{i."+propertyName.get(0)+"},");
            }else if(i==propertyName.size()-1){
                System.out.println(" now())");
            }else if(i==propertyName.size()-2){
                System.out.print("now(),");
            }else{
                System.out.print("#{i."+propertyName.get(i)+"},");
            }
        }
        System.out.println("</foreach>");
        System.out.println("</insert>");
    }
    public static void delete(){
        System.out.println(" <delete id=\"delete\" parameterType=\"long\">");
        System.out.println("DELETE FROM");
        System.out.println("<include refid=\"tb\"/>");
        System.out.println("WHERE id = #{id}");
        System.out.println("</delete>");
    }
    public static void load(String className){
        System.out.println("<select id=\"load\" parameterType=\"long\" resultMap=\""+className+"Map\">");
        System.out.println("SELECT");
        System.out.println("<include refid=\"cols_all\"/>");
        System.out.println("FROM");
        System.out.println("<include refid=\"tb\"/>");
        System.out.println("WHERE id = #{id}");
        System.out.println("</select>");
    }
    public static void list(String className){
        System.out.println("<select id=\"list\" parameterType=\"map\" resultMap=\""+className+"Map\">");
        System.out.println("SELECT");
        System.out.println("<include refid=\"cols_all\"/>");
        System.out.println("FROM <include refid=\"tb\"/>");
        System.out.println("<where>");
        System.out.println("<include refid=\"criteria\"/>");
        System.out.println("</where>");
        System.out.println("ORDER BY id DESC");
        System.out.println("</select>");

    }
    public static void update(String className){
        System.out.println("<update id=\"update\" parameterType=\""+className+"\">");
        System.out.println("UPDATE");
        System.out.println("<include refid=\"tb\"/>");
        System.out.println("<include refid=\"update_sql\"/>");
        System.out.println("WHERE id = #{id}");
        System.out.println("</update>");
    }
    public static void paging(String className){
        System.out.println("<select id=\"paging\" parameterType=\"map\" resultMap=\""+className+"Map\">");
        System.out.println("SELECT");
        System.out.println("<include refid=\"cols_all\"/>");
        System.out.println("FROM");
        System.out.println("<include refid=\"tb\"/>");
        System.out.println("<where>");
        System.out.println("<include refid=\"criteria\"/>");
        System.out.println("</where>");
        System.out.println("ORDER BY id DESC LIMIT #{offset}, #{limit}");
        System.out.println("</select>");
    }
    public static void count(){
        System.out.println("<select id=\"count\" parameterType=\"map\" resultType=\"long\">");
        System.out.println("SELECT COUNT(1)");
        System.out.println("FROM");
        System.out.println("<include refid=\"tb\"/>");
        System.out.println("<where>");
        System.out.println("<include refid=\"criteria\"/>");
        System.out.println("</where>");
        System.out.println("</select>");
    }
}
